<template>
  <div class="back-top" @click="back" v-show="visible">
    TOP
  </div>
</template>

<script>
export default {
  name: 'BackTop',
  data() {
    return {
      visible: false,
    }
  },
  props: {
    container: {
      type: String,
      default() {
        return 'main'
      },
    },
  },
  methods: {
    back() {
      // console.log('回到顶部...')
      document.querySelector(this.container).scrollTop = 0
    },
    handleScroll(event) {
      // console.log('滚动了...', event.target.scrollTop)
      this.visible = event.target.scrollTop >= 1000
    },
  },
  mounted() {
    document.querySelector(this.container).addEventListener('scroll', this.handleScroll, false)
    // console.log('top mounted.......')
    // window.onscroll = function() {
    //   console.log('滚动了...')
    // }
    // console.log('top mounted.......', window.onscroll)
  },
  beforeDestroy() {
    document.querySelector(this.container).removeEventListener('scroll', this.handleScroll, false)
  },
}
</script>

<style lang="less" scoped>
  .back-top {
    width: 80px;
    height: 80px;
    line-height: 80px;
    text-align: center;
    background: #00f;
    color: #fff;
    font-size: 18px;
    position: fixed;
    bottom: 120px;
    right: 20px;
    border-radius: 40px;
  }
</style>
